package net.cardgame.orcalecard;

import jp.jma.oraclecard.R;
import net.cardgame.oraclecard.common.ThreadLoadImage;
import net.cardgame.orcalecard.bean.SettingApp;
import net.cardgame.orcalecard.utils.ConstantValue;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class SplashDeckBeanActivity extends Activity implements
		AnimationListener, OnClickListener {
	ImageView img_ring;
	int deckId;
	int state_machine = 0;
	boolean clickable = true;
	MediaPlayer mediaPlayer;
	SettingApp setting;
	ImageView img_bg, image_scale, img_deckbean_info;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_deckbean_activity);
		setting = ConstantValue.getSettingApp(this);
		// get deck bean Id & number to get card
		Bundle bundle = getIntent().getExtras();
		deckId = bundle.getInt("deckId");
		img_bg = (ImageView) findViewById(R.id.image_bg_splash_deck);
		image_scale = (ImageView) findViewById(R.id.img_scale);
		img_deckbean_info = (ImageView) findViewById(R.id.image_deckbean_info);
		img_ring = (ImageView) findViewById(R.id.img_ef_ring);
		findViewById(R.id.btnhelp_splash_deckbean).setOnClickListener(this);
		findViewById(R.id.btnBack_splash_deckbean).setOnClickListener(this);
		loadImage();
	}

	void loadImage() {
		if (deckId == 999) {
			img_bg.setImageResource(R.drawable.bg999);
			img_deckbean_info.setImageResource(R.drawable.c999m);
			image_scale.setImageResource(R.drawable.c999p);
			Animation hyperspaceJump = AnimationUtils.loadAnimation(
					SplashDeckBeanActivity.this, R.anim.scale_animation);
			hyperspaceJump.setAnimationListener(this);
			image_scale.startAnimation(hyperspaceJump);

			return;
		}
		String id = deckId < 10 ? "0" + deckId : "" + deckId;
		String patchBg = ConstantValue.getPatchCardData(this) + "Card_" + id
				+ "/en_bg" + id + ".jpg";
		String patchImageScale = ConstantValue.getPatchCardData(this) + "Card_"
				+ id + "/en_c" + id + "p.png";
		String patchImageInfo = ConstantValue.getPatchCardData(this) + "Card_"
				+ id + "/en_c" + id + "m.png";
		new ThreadLoadImage(this, handler, patchBg, img_bg, 1).start();
		new ThreadLoadImage(this, handler, patchImageInfo, img_deckbean_info, 2)
				.start();
		new ThreadLoadImage(this, handler, patchImageScale, image_scale, 3)
				.start();
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				img_bg.setImageDrawable((Drawable) msg.obj);
				Animation hyperspaceJump = AnimationUtils.loadAnimation(
						SplashDeckBeanActivity.this, R.anim.scale_animation);
				hyperspaceJump
						.setAnimationListener(SplashDeckBeanActivity.this);
				image_scale.startAnimation(hyperspaceJump);
				break;
			case 2:
				img_deckbean_info.setImageDrawable((Drawable) msg.obj);
				break;
			case 3:
				image_scale.setImageDrawable((Drawable) msg.obj);
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onAnimationEnd(Animation animation) {
		// TODO Auto-generated method stub
		switch (state_machine) {
		case 0:
			state_machine = 1;
			img_ring.setImageResource(R.drawable.ef_ring);
			Animation ef_ring = AnimationUtils.loadAnimation(this,
					R.anim.ring_animation);
			ef_ring.setAnimationListener(this);
			img_ring.startAnimation(ef_ring);
			break;
		case 1:
			state_machine = 2;
			img_ring.setVisibility(View.INVISIBLE);
			image_scale.setOnClickListener(this);
			break;
		case 2:
			img_ring.setVisibility(View.INVISIBLE);
			gotoGetCard();
			break;
		default:
			break;
		}

	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (!clickable)
			return;
		switch (v.getId()) {
		case R.id.btnhelp_splash_deckbean:// go to help Activity
			clickable = false;
			Intent i = new Intent(this, HelpActivity.class);
			i.putExtra("indexTypeHelp", 8);
			startActivityForResult(i, 5);
			overridePendingTransition(R.anim.slide_in_right,
					R.anim.slide_out_left);
			break;
		case R.id.btnBack_splash_deckbean:
			clickable = false;
			backtoCardDeckActivity();
			break;
		case R.id.img_scale: // go to get card Activity
			clickable = false;
			if (setting.sound) {
				new ThreadPlayMusic(this).start();
			}
			img_ring.setVisibility(View.VISIBLE);
			Animation ef_ring = AnimationUtils.loadAnimation(this,
					R.anim.ring_animation);
			ef_ring.setAnimationListener(this);
			img_ring.startAnimation(ef_ring);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		clickable = true;
		overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		backtoCardDeckActivity();
	}

	private void gotoGetCard() {
		Intent intent = new Intent(this, GetCardActivity.class);
		intent.putExtras(getIntent().getExtras());
		startActivity(intent);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
		this.finish();
	}

	private void backtoCardDeckActivity() {
		Intent intent = new Intent(this, CardDeckActivity.class);
		intent.putExtras(getIntent().getExtras());
		startActivity(intent);
		overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
		this.finish();
	}

	class ThreadPlayMusic extends Thread {
		Context mContext;

		ThreadPlayMusic(Context context) {
			mContext = context;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			MediaPlayer mediaPlayer = MediaPlayer.create(mContext,
					R.raw.se_deck_clearing);
			mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					// TODO Auto-generated method stub
					mp.start();
				}
			});
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					mp.release();
				}
			});
		}
	}
}
